﻿using BW.ME.Exam.Domain.Entities;
using BW.ME.Exam.ErrorCode;
using BW.ME.Exam.Infrastructure.Interfaces;
using BW.ME.Exam.Read.Api.Applications.Commands.UserLoginCommand;
using BW.ME.Exam.Read.Api.Dtos;
using MD5Hash;
using MediatR;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Cryptography;
using System.Text;

namespace BW.ME.Exam.Read.Api.Applications.CommandHandlers.UserLoginCommandHandler
{
    /// <summary>
    /// 登录查询命令处理
    /// </summary>
    public class LoginQueryCommandHandler : IRequestHandler<LoginQueryCommand, ApiResult<UserDto>>
    {
        public LoginQueryCommandHandler(IBaseRepository<User> UserRepository,IBaseRepository<Role> RoleRepository,IBaseRepository<UserRole> UserRoleRepository)
        {
            this.UserRepository = UserRepository;
            this.RoleRepository = RoleRepository;
            this.UserRoleRepository = UserRoleRepository;
        }

        public IBaseRepository<User> UserRepository { get; }
        public IBaseRepository<Role> RoleRepository { get; }
        public IBaseRepository<UserRole> UserRoleRepository { get; }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<ApiResult<UserDto>> Handle(LoginQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<UserDto> result=new ApiResult<UserDto>();
            result.Code = ResultCode.登录成功;
            result.Message=ResultCode.登录成功.ToString();
            //异常处理
            try
            {
                
                  var list = from a in UserRepository.GetAll()
                           join b in UserRoleRepository.GetAll() on a.UserId equals b.UserId
                           join c in RoleRepository.GetAll() on b.RoleId equals c.RoleId
                           where a.UserName == request.UserName
                           select new UserDto
                           {
                               UserId = a.UserId,
                               UserName = a.UserName,
                               UserPass=a.UserPass,
                               UserNickname = a.UserNickname,
                               RoleId = c.RoleId,
                               RoleName = c.RoleName,
                               
                           };
                if (list == null || list.Count() == 0)
                {
                    throw new Exception("用户名不存在");
                }
                var entity=list.FirstOrDefault();
                request.UserPass = request.UserPass.GetMD5();
                if (entity.UserPass !=request.UserPass)
                {
                    throw new Exception("用户名和密码不匹配");
                }
                result.Data=entity;
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.登录失败;
                result.Message= ResultCode.登录失败.ToString()+""+ex.Message;
            }

            return Task.FromResult(result);
        }




        ///// <summary>
        ///// 获取MD5
        ///// </summary>
        ///// <param name="scource"></param>
        ///// <returns>返回字符串</returns>
        //public string GetMD5(string scource)
        //{
        //    var md5 = MD5.Create();
        //    byte[] bytes=Encoding.UTF8.GetBytes(scource);
        //    byte[] hash=md5.ComputeHash(bytes);
        //    string target = BitConverter.ToString(hash, 0, hash.Length).Replace("-","");
        //    return target;
        //}




       
    }
}
